#include <send.h>

/*
 * 函数名:send_M0
 * 功能:向M0发送数据
 * 参数:dev *dev:设备控制结构体
 * 返回值:成功返回OK,失败返回ERR
 */
 
 extern int zgbfd;

int send_M0(env_dev_rfid_card data)
{
	volatile int aaa = 0;
	printf("send_M0########## : rfid_card=%d\t air_fan=%d\tsunshade=%d\tspray_water=%d\n", data.rfid_card, data.dev.air_fan, data.dev.sunshade, data.dev.spray_water);
	int ret = 0;

	if (0 == data.rfid_card)
	{
		if(0 == data.dev.air_fan)
		{
			ret = zigbee_exe_cmd(zgbfd,"FAN_OFF");
		}
		else if(1 == data.dev.air_fan)
		{
			ret = zigbee_exe_cmd(zgbfd,"FAN_ON");
		}	

		if(0 == data.dev.sunshade)
		{		
			ret = zigbee_exe_cmd(zgbfd,"LIGHT_OFF");
		}
		else if(1 == data.dev.sunshade)
		{
			ret = zigbee_exe_cmd(zgbfd,"LIGHT_ON");
		}

		if(0 == data.dev.spray_water)
		{
			ret = zigbee_exe_cmd(zgbfd,"BUZZ_OFF");
		}
		else if(1 == data.dev.spray_water)
		{
			ret = zigbee_exe_cmd(zgbfd,"BUZZ_ON");
		}
	}
	else if(-1 == data.rfid_card)
	{
		ret = zigbee_exe_cmd(zgbfd,"SHU_ON");
		aaa = 1;	
		sleep(2);
	}
	else if(-2 == data.rfid_card)
	{
		ret = zigbee_exe_cmd(zgbfd,"DOR_ON");
		sleep(3);
		ret = zigbee_exe_cmd(zgbfd,"DOR_OFF");
	}
		
	if (aaa == 1)
	{
		ret = zigbee_exe_cmd(zgbfd,"SHU_OFF");
		aaa = 0;
	}
	
	if(0 < ret)
	{
		return ERR;
	}
	return OK;

	
	
	
	/*
	unsigned char *p = NULL;
	//printf("@@@@@air_fan=%d\tsunshade=%d\tspray_water=%d\n", data.dev.air_fan, data.dev.sunshade, data.dev.spray_water);
	int ret = data.rfid_card[4];
	
	if('0' == data.dev.air_fan){
		p = "FAN_OFF";
	}
	else if('1' == data.dev.air_fan){
		p = "FAN_ON";
	}
	//printf("@@@@@@@@@@p=%s\n", p);
	ret = zigbee_exe_cmd(zgbfd,p);
	sleep(3);
	if('0' == data.dev.sunshade){
		p = "LIGHT_OFF";		
	}
	else if('1' == data.dev.sunshade){
		p = "LIGHT_ON";
	}
	//printf("@@@@@@@@@@p=%s\n", p);
	ret = zigbee_exe_cmd(zgbfd,p);
	sleep(3);
	if('0' == data.dev.spray_water){
		p = "SHU_OFF";
	}
	else if('1' == data.dev.spray_water){
		p = "SHU_ON";
	}
	//printf("@@@@@@@@@@p=%s\n", p);
	ret = zigbee_exe_cmd(zgbfd,p);
	sleep(3);
	if(data.rfid_card[0] == 0xCC && 1 == data.rfid_card[7]  && 0 == data.rfid_card[6] && 33 == data.rfid_card[5] && 18 == data.rfid_card[4])
	{
		
		//printf("111111\n");		
		//memset(data.rfid_card,0,RFID_CARD_NUM_SIZE);
		p = "SHU_ON";
		zigbee_exe_cmd(zgbfd, p);
		sleep(2);
		p = "SHU_OFF";
		ret = zigbee_exe_cmd(zgbfd, p);
	}
	else
	{
		p = "SHU_ON";
		zigbee_exe_cmd(zgbfd, p);
		sleep(2);
		p = "SHU_OFF";
		ret = zigbee_exe_cmd(zgbfd, p);
	}

	if(0 < ret)
	{
		return ERR;
	}
	return OK;
	*/
}